package com.sohoappspot.sohosched.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

public class ApplicationLayout extends Composite implements ResizeHandler {

	private static ApplicationLayoutUiBinder uiBinder = GWT
			.create(ApplicationLayoutUiBinder.class);

	interface ApplicationLayoutUiBinder extends
			UiBinder<Widget, ApplicationLayout> {
	}

	/**
	 * Images used in the {@link Application}.
	 */
	public interface ApplicationImages extends Tree.Resources {
		/**
		 * An image indicating a leaf.
		 * 
		 * @return a prototype of this image
		 */
		@Source("noimage.png")
		ImageResource treeLeaf();
	}

	/**
	 * The last known width of the window.
	 */
	private int windowWidth = -1;

	/**
	 * The base style name.
	 */
	public static final String DEFAULT_STYLE_NAME = "Application";

	@UiField
	FlowPanel layout;
	@UiField
	FlexTable topPanel;
	@UiField
	HorizontalPanel linksPanel;
	@UiField
	HorizontalPanel bottomPanel;
	@UiField
	VerticalPanel mainMenu;
	@UiField
	DecoratorPanel contentDecorator;
	@UiField
	Grid contentLayout;
	@UiField
	SimplePanel contentWrapper;

	public ApplicationLayout() {
		initWidget(uiBinder.createAndBindUi(this));

		// Setup TopPanel
		setupTopPanel();

		// Add to main layout widget
		layout.add(topPanel);

		// Add Bottom Panel
		layout.add(bottomPanel);

		// Format Content Layour
		contentLayout = new Grid(2, 1);
		contentDecorator
				.addStyleName(DEFAULT_STYLE_NAME + "-content-decorator");
		contentDecorator.setWidget(contentLayout);

		// i18n settings
		if (LocaleInfo.getCurrentLocale().isRTL()) {
			bottomPanel.setCellHorizontalAlignment(contentDecorator,
					HasHorizontalAlignment.ALIGN_LEFT);
			contentDecorator.getElement().setAttribute("align", "LEFT");
		} else {
			bottomPanel.setCellHorizontalAlignment(contentDecorator,
					HasHorizontalAlignment.ALIGN_RIGHT);
			contentDecorator.getElement().setAttribute("align", "RIGHT");
		}
		CellFormatter formatter = contentLayout.getCellFormatter();

		// Set where main content is to go
		contentLayout.setWidget(1, 0, contentWrapper);
		formatter.setStyleName(1, 0, DEFAULT_STYLE_NAME + "-content-wrapper");

		bottomPanel.add(contentDecorator);
	}

	/**
	 * Add a link to the top of the page.
	 * 
	 * @param link
	 *            the widget to add to the mainLinks
	 */
	public void addLink(Widget link) {
		if (linksPanel.getWidgetCount() > 0) {
			linksPanel.add(new HTML("&nbsp;|&nbsp;"));
		}
		linksPanel.add(link);
	}

	/**
	 * Create the panel at the top of the page that contains the title and
	 * links.
	 */
	// private void createTopPanel() {
	private void setupTopPanel() {
		boolean isRTL = LocaleInfo.getCurrentLocale().isRTL();
		FlexCellFormatter formatter = topPanel.getFlexCellFormatter();

		// Setup the links cell
		// linksPanel = new HorizontalPanel();
		topPanel.setWidget(0, 0, linksPanel);
		formatter.setStyleName(0, 0, DEFAULT_STYLE_NAME + "-links");
		if (isRTL) {
			formatter.setHorizontalAlignment(0, 0,
					HasHorizontalAlignment.ALIGN_LEFT);
		} else {
			formatter.setHorizontalAlignment(0, 0,
					HasHorizontalAlignment.ALIGN_RIGHT);
		}
		formatter.setColSpan(0, 0, 2);

		// Setup the title cell
		// ToDo
		// setTitleWidget(null);
		formatter.setStyleName(1, 0, DEFAULT_STYLE_NAME + "-title");

		// Setup the options cell
		// ToDo
		// setOptionsWidget(null);
		formatter.setStyleName(1, 1, DEFAULT_STYLE_NAME + "-options");
		if (isRTL) {
			formatter.setHorizontalAlignment(1, 1,
					HasHorizontalAlignment.ALIGN_LEFT);
		} else {
			formatter.setHorizontalAlignment(1, 1,
					HasHorizontalAlignment.ALIGN_RIGHT);
		}

		// Align the content to the top
		topPanel.getRowFormatter().setVerticalAlign(0,
				HasVerticalAlignment.ALIGN_TOP);
		topPanel.getRowFormatter().setVerticalAlign(1,
				HasVerticalAlignment.ALIGN_TOP);
	}

	/**
	 * @return the {@link Widget} in the content area
	 */
	public Widget getContent() {
		return contentWrapper.getWidget();
	}

	/**
	 * @return the content title widget
	 */
	public Widget getContentTitle() {
		return contentLayout.getWidget(0, 0);
	}

	/**
	 * @return the main menu.
	 */
	public VerticalPanel getMainMenu() {
		return mainMenu;
	}

	/**
	 * @return the {@link Widget} used as the title
	 */
	public Widget getTitleWidget() {
		return topPanel.getWidget(0, 0);
	}

	public void onResize(ResizeEvent event) {
		onWindowResized(event.getWidth(), event.getHeight());
	}

	public void onWindowResized(int width, int height) {
		if (width == windowWidth || width < 1) {
			return;
		}
		windowWidth = width;
		onWindowResizedImpl(width);
	}

	/**
	 * Set the {@link Widget} to display in the content area.
	 * 
	 * @param content
	 *            the content widget
	 */
	public void setContent(Widget content) {
		if (content == null) {
			contentWrapper.setWidget(new HTML("&nbsp;"));
		} else {
			contentWrapper.setWidget(content);
		}
	}

	/**
	 * Set the title of the content area.
	 * 
	 * @param title
	 *            the content area title
	 */
	public void setContentTitle(Widget title) {
		contentLayout.setWidget(0, 0, title);
	}

	/**
	 * Set the {@link Widget} to use as options, which appear to the right of
	 * the title bar.
	 * 
	 * @param options
	 *            the options widget
	 */
	public void setOptionsWidget(Widget options) {
		topPanel.setWidget(1, 1, options);
	}

	/**
	 * Set the {@link Widget} to use as the title bar.
	 * 
	 * @param title
	 *            the title widget
	 */
	public void setTitleWidget(Widget title) {
		topPanel.setWidget(1, 0, title);
	}

	@Override
	protected void onLoad() {
		super.onLoad();
		onWindowResized(Window.getClientWidth(), Window.getClientHeight());
	}

	@Override
	protected void onUnload() {
		super.onUnload();
		windowWidth = -1;
	}

	protected void onWindowResizedImpl(int width) {
		int menuWidth = mainMenu.getOffsetWidth();
		int contentWidth = Math.max(width - menuWidth - 30, 1);
		int contentWidthInner = Math.max(contentWidth - 10, 1);
		bottomPanel.setCellWidth(mainMenu, menuWidth + "px");
		bottomPanel.setCellWidth(contentDecorator, contentWidth + "px");
		contentLayout.getCellFormatter().setWidth(0, 0,
				contentWidthInner + "px");
		contentLayout.getCellFormatter().setWidth(1, 0,
				contentWidthInner + "px");
	}

	// @return the main menu.
	public VerticalPanel getMainMenuPanel() {
		return mainMenu;
	}

}
